<?php
namespace Api\Service;

/**
 * 店铺
 */
class ShopService
{
    const LOW_STAR  = 0;
    const HIGH_STAR = 5;

    /**
     * 店铺信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function info($id)
    {
        $shop = M('shop')
            ->field('id,name,logo,image,description,goods_match,shop_service,express_service')
            ->find($id);
        if (!$shop) {
            return [];
        }

        $shop['goods_match_star']     = $this->star($shop['goods_match']);
        $shop['shop_service_star']    = $this->star($shop['shop_service']);
        $shop['express_service_star'] = $this->star($shop['express_service']);

        return $shop;
    }

    /**
     * 店铺评分
     * @param  [type] $scount [description]
     * @return [type]         [description]
     */
    protected function star($scount)
    {
        $star = 0;

        $ranges = [self::LOW_STAR, setting('shop_low_middle'), setting('shop_middle_high'), self::HIGH_STAR];
        for ($i = 0; $i < count($ranges) - 1; $i++) {
            if ($ranges[$i] <= $scount && $scount < $ranges[$i + 1]) {
                $star = $i;
                break;
            }
        }

        switch ($star) {
            case 0:
                return '低';
            case 1:
                return '中';
            case 2:
                return '低';
            default:
                return;
        }
    }

    /**
     * 商城推荐店铺
     * @return [type] [description]
     */
    public function top()
    {
        $shop = S('top_article');
        if (!$shop || true) {

            $shop = M('shop')
                ->field('id,name,logo,description')
                ->where([
                    'is_top'  => 1,
                    'is_open' => 1,
                ])->find();

            if (!$shop) {
                return [];
            }

            $shop['goods'] = D('Api/Goods', 'Service')->getNewByShopId($shop['id']);

            S('top_article', $shop, 3600); // 缓存一小时
        }

        return $shop;
    }
}
